package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.bean.AddDeviceBean;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/09/22
 *    desc   : 状态数据列表
 */
public final class AddDeviceTypeAdapter extends AppAdapter<AddDeviceBean> {

    private TextView lastSelectedView;

    public AddDeviceTypeAdapter(@NonNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private ViewHolder() {
            super(R.layout.item_add_device_type);
        }

        @Override
        public void onBindView(int position) {
            AddDeviceBean item = getItem(position);
            ((TextView)itemView).setText(item.getProductClassName());
        }
    }

    @Override
    public long getItemId(int position) {
        return getData().get(position).hashCode();
    }

    public void selectItem(int position){
        RecyclerView.ViewHolder viewHolder = getRecyclerView().findViewHolderForLayoutPosition(position);
        TextView item = (TextView) viewHolder.itemView;
        if(item != null && item != lastSelectedView){
            item.setSelected(true);
            item.setTypeface(Typeface.DEFAULT_BOLD);
            if(lastSelectedView != null){
                lastSelectedView.setSelected(false);
                lastSelectedView.setTypeface(Typeface.DEFAULT);
            }
            lastSelectedView = item;
        }
    }
}